package cn.wolfcode.web.controller;

import cn.wolfcode.annotation.PermissionName;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName EmployeeController
 * Create by Mike
 * Date 2021/8/1 16:52
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @PermissionName("员工查询")
    @RequiresPermissions("employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        return "/employee/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id){
        model.addAttribute("departments",departmentService.listAll());
        model.addAttribute("allRoles",roleService.listAll());
        if (id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
            model.addAttribute("ownRoles",roleService.queryByEmployeeId(employee.getId()));
        }
        return "/employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @PermissionName("员工新增/修改")
    @RequiresPermissions("employee:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] roleIds){
        try {
            if (employee.getId() == null){
                employeeService.save(employee,roleIds);
            }else {
                employeeService.update(employee,roleIds);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
        return new JsonResult(true,"操作成功");
    }

    @RequestMapping("/delete")
    @PermissionName("员工删除")
    @RequiresPermissions("employee:delete")
    public String delete(Long id){
        if (id != null){
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }

    @ResponseBody
    @RequestMapping("/checkUserName")
    public Map<String,Boolean> checkUsername(String username){
        //如若存在就返回true,不存在返回false
        boolean exist = employeeService.existUsername(username);
        HashMap<String, Boolean> data = new HashMap<>();
        data.put("valid",!exist);
        return data;
    }

}
